<?php

/**
 * Center actions.
 *
 * @package    5
 * @subpackage Center
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */

class CenterActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  }
	public function executeAddfund(sfWebRequest $request){
		$this->form = new PointLogForm();
		if ($request->isMethod('post')){
			$this->form->bind($request->getParameter($this->form->getName()));
			if ($this->form->isValid())
			{
				$orderno = rand(10000000,99999999);
				$this->form->getObject()->setType(2);
				$this->form->getObject()->setOrderNo($orderno);
				$this->form->getObject()->setAction(1);
				$point = $this->form->save();
				if($point){
					$this->redirect('@funded?id='.$point);
				}
			}else{
				$this->message = "订单错误！";
			}
			$this->setTemplate('pay');
		}
	}
	public function executeVerify(sfWebRequest $request){
		$code = $_GET['code'];
		$user = Doctrine_Core::getTable('sfGuardUser')
			->createQuery('u')
			->where('u.verifycode = ?',$code)
			->fetchOne();
		if($user){
			$user->setVerifycode('')->setIsActive(1)->save();
			$inviter = $user->getInviter();
			if ($iid = $inviter->getId()) {
				Doctrine_Core::getTable('sfGuardUser')->find(array($iid))
					->gainPoint(sfConfig::get('app_point_invitation'), PointLog::TYPE_INVITATION);
			}
			$this->message = '邮箱验证成功!';
			$this->redirect('@center');
		}else{
			$this->getUser()->setFlash('notice', '参数错误!');
			$this->redirect('@homepage');
		}
	}
	public function executeFunded(sfWebRequest $request)
  {
	$point = Doctrine_Core::getTable('PointLog')->find($request->getParameter('id'));
	if(!$point){
		$this->message = "订单错误！";
		$this->setTemplate('pay');
	}else{
		$out_trade_no = $point->getOrderNo();	//请与贵网站订单系统中的唯一订单号匹配
		$subject      = "账户充值";	//订单名称
		$body         = "账户充值"; //订单描述
		$total_fee    = $point->getPoint();	//订单总金额
		
		$parameter = array(
			"service"			=> "create_direct_pay_by_user",	//接口名称，不需要修改
			"payment_type"		=> "1",               			//交易类型，不需要修改

			//获取配置文件(app.yml)中的值
			"partner"			    => sfConfig::get('app_pay_partner'),
			"seller_email"	  => sfConfig::get('app_pay_email'),
			"return_url"		  => sfConfig::get('app_pay_return_url'),
			"notify_url"		  => sfConfig::get('app_pay_notify_url'),
			"_input_charset"	=> sfConfig::get('app_pay_input_charset'),
			"show_url"			  => sfConfig::get('app_pay_site_url'),

			//从订单数据中动态获取到的必填参数
			"out_trade_no"		=> $out_trade_no,
			"subject"			    => $subject,
			"body"				    => $body,
			"total_fee"			  => $total_fee
		  //订单超时时间
		  //"it_b_pay"	      => "1d"
		);


		$key = sfConfig::get('app_pay_key');
		$sign_type = sfConfig::get('app_pay_sign_type');
		$order = new aliPay($parameter,$key,$sign_type);
		$sHtmlText = $order->build_form();
		return $this->renderText($sHtmlText);
	}
  }
  public function executePaynotify(sfWebRequest $request)
  {
    //支付宝配置
    $partner = sfConfig::get('app_pay_partner');
    $key = sfConfig::get('app_pay_key');
	$sign_type = sfConfig::get('app_pay_sign_type');
	$_input_charset = sfConfig::get('app_pay_input_charset');
	$transport = sfConfig::get('app_pay_transport');

    $alipay = new aliPaynotify($partner,$key,$sign_type,$_input_charset,$transport);
    $notify_verify = $alipay->notify_verify();
    if($notify_verify) {
        $out_order_no = $_POST['out_trade_no']; //订单号
        $order_status = $_POST['trade_status'];//交易状态

        if($order_status == 'TRADE_FINISHED' ||$order_status == 'TRADE_SUCCESS') {    //交易成功结束
            $point = Doctrine::getTable('PointLog')->createQuery('p')
              ->where('p.order_no = ?', $out_order_no)
              ->addWhere('i.status = ?', 0)
              ->fetchOne();
            if($point){
              $point->setStatus(1)->save();
              //处理用户余额
			  $user = $point->getUser();
			  $user->setPoint($user->getPoint()+$point->getPoint())->save();
            }
            return $this->renderText("success");
        }
        else {
            return $this->renderText("success");		//其他状态判断。普通即时到帐中，其他状态不用判断，直接打印success。
        }
    }
    else {
        return $this->renderText("fail");
    }
    //$this->setTemplate('payreturn');
  }
  public function executePayreturn(sfWebRequest $request)
	{
		//支付完成跳转页面，完成订单数据更新。
		$partner = sfConfig::get('app_pay_partner');
		$key = sfConfig::get('app_pay_key');
		$sign_type = sfConfig::get('app_pay_sign_type');
		$_input_charset = sfConfig::get('app_pay_input_charset');
		$transport = sfConfig::get('app_pay_transport');

		$alipay = new aliPaynotify($partner,$key,$sign_type,$_input_charset,$transport);
		$return_verify = $alipay->return_verify();
		if($return_verify){
			$this->out_order_no = $request->getParameter('out_trade_no'); //订单号
			$this->order_no = $request->getParameter('trade_no');//淘宝交易号
			$this->order_total = $request->getParameter('total_fee');//订单总额
			$this->order_title = $request->getParameter('subject');//订单标题
			$this->order_ps = $request->getParameter('body');//订单描述
			$this->order_bemail = $request->getParameter('buyer_email');//买家Email
			$order_status = $request->getParameter('trade_status');//交易状态
			if($order_status == 'TRADE_FINISHED' || $order_status == 'TRADE_SUCCESS')
			{
				$point = Doctrine::getTable('PointLog')->createQuery('i')
				->where('i.order_no = ?', $this->out_order_no)
				->fetchOne();
				if($point->getStatus()==0){
					$point->setStatus(1)->save();
					$user = $point->getUser();
					$user->setPoint($user->getPoint()+$point->getPoint())->save();
				}
				$this->message = "充值成功！";
			}else{
				$this->message = "充值失败，如有疑问，请与我们联系！";
			}
		}else{
			$this->message = "参数错误！";
		}
		$this->setTemplate('pay');
	}
}
